﻿#region Usings

using System;
using Kazkommertsbank.Framework.Model;
using Kazkommertsbank.Framework.Model.Pay;
using Kazkommertsbank.Framework.Test.Components;
using log4net;

#endregion

namespace Kazkommertsbank.Framework.Test
{
    /// <summary>
    ///   Summary description for KkbHandler
    /// </summary>
    public class KkbHandler : KkbResponseHandlerBase
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(KkbResponseHandlerBase));

        #region Overrides of KkbResponseHandlerBase

        protected override void ProcessKkbPaymentResponse(ResponseDocumentBase paymentResponse)
        {
            logger.Info("Start processing KKB Response");
            if(paymentResponse == null)
            {
                logger.Error("paymentResponse = NULL!");
                return;
            }

            try
            {
                switch(paymentResponse.ResponseType)
                {
                    case KkbResponseType.Error:
                        PaymentErrorDocument errorDocument = paymentResponse as PaymentErrorDocument;
                        if(errorDocument != null)
                            logger.InfoFormat("Error Callback XML{0}{1}", Environment.NewLine, errorDocument.ResponseXml);
                        break;
                    case KkbResponseType.Success:
                        PaymentSuccessDocument successDocument = paymentResponse as PaymentSuccessDocument;
                        if(successDocument != null)
                        {
                            IKkbSettings settings = KkbSettings.Init();
                            KkbController controller = new KkbController(settings);
                            logger.InfoFormat("Success Callback XML{0}{1}{0}", Environment.NewLine, successDocument.ResponseXml);
                            logger.InfoFormat("Verification: {0}", controller.Verify(successDocument.Bank.ToXmlString(), successDocument.BankSign));
                        }

                        break;
                }
            }
            catch(Exception ex)
            {
                logger.Fatal("ProcessKkbPaymentResponse", ex);
            }
        }

        #endregion
    }
}